package cn.sinata.wanglin.mine.activity;
import android.content.Intent;
import android.net.Uri;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;

import com.google.gson.JsonObject;
import com.xilada.xldutils.activitys.CropImageActivity;
import com.xilada.xldutils.activitys.SelectPhotoDialog;
import com.xilada.xldutils.activitys.TitleActivity;
import com.xilada.xldutils.utils.ActivityUtil;
import com.xilada.xldutils.utils.SharedPreferencesUtils;
import com.xilada.xldutils.utils.Utils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import cn.sinata.wanglin.R;
import cn.sinata.wanglin.aliyunoss.OssBean;
import cn.sinata.wanglin.aliyunoss.UpLoadImageUtils;
import cn.sinata.wanglin.commonutils.Constant;
import cn.sinata.wanglin.net.HttpManager;
import cn.sinata.wanglin.net.model.util.ResultDataSubscriber;
import rx.Observable;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action0;

public class AuthenticationCareerActivity extends TitleActivity {

    private String pathurl=null;//学历
    @BindView(R.id.iv_upload_card)
    ImageView iv_upload_card;//学历照片显示
    @Override
    protected int setContentId() {
        return R.layout.activity_authentication_career;
    }
    @Override
    protected void initView() {
        super.initView();
        setTitle("事业认证");
        addRightButton("提交认证", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                commitData();
            }
        });
    }
    /**
     * 上传数据
     */
    private void commitData() {
        if(TextUtils.isEmpty(pathurl)){
            showToast("个人名片不能为空！");
            return;
        }

        updateFile();
    }
    private void updateFile(){
        Utils.systemErr("-pathurl-->"+pathurl);
        final ArrayList<OssBean> mOssBeans=new ArrayList<>();
        mOssBeans.add(new OssBean(pathurl));
        Observable<List<OssBean>> mObservable= new UpLoadImageUtils(mOssBeans).uploadimg();
        mObservable.doOnSubscribe(new Action0() {
            @Override
            public void call() {
                showDialog("上传中...");
            }
        })
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<List<OssBean>>() {
                    @Override
                    public void onCompleted() {
                        dismissDialog();
                    }
                    @Override
                    public void onError(Throwable e) {
                    }

                    @Override
                    public void onNext(List<OssBean> ossBeen) {
                        if(ossBeen!=null&&ossBeen.size()!=0){
                            uploadMyinfo(ossBeen.get(0).getUrlkey());
                        }
                    }
                });


    }

    private void uploadMyinfo(String urlkey) {
        String userid= SharedPreferencesUtils.getString(Constant.SP_USERID);
        HttpManager.submitApprove(userid,"D",null,null,null,urlkey).subscribe(new ResultDataSubscriber<JsonObject>(this) {
            @Override
            public void onSuccess(String msg, JsonObject jsonObject) {
                showToast(msg);
                finish();
            }
        });
    }

    @OnClick({R.id.iv_upload_card})
    void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_upload_card:
                //描述:个人名片
                ActivityUtil.create(this).go(SelectPhotoDialog.class).startForResult(1);
                break;

        }
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(resultCode==RESULT_OK){
            switch (requestCode){
                case 6:
                    if (data!=null){
                        pathurl = data.getStringExtra("path");
                        iv_upload_card.setImageURI(Uri.parse("file://" + pathurl));
                    }
                    break;
                case 1:
                    if (data!=null){
                        String path = data.getStringExtra(SelectPhotoDialog.DATA);
                        cropImage(path);
                    }
                    break;
            }

        }
    }
    private void cropImage(String path){
        ActivityUtil.create(this).go(CropImageActivity.class)
                .put("uri",path)
                .put("mode",0)
                .startForResult(6);
    }
}
